/**
 * @作者 zxy
 * @时间 2023-04-28 16:51
 * @说明 999. 可以被一步捕获的棋子数
 * 示例 1：
 * 输入：[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
 * 输出：3
 * 解释：
 * 在本例中，车能够捕获所有的卒。
 * 示例 2：
 * 输入：[[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
 * 输出：0
 * 解释：
 * 象阻止了车捕获任何卒。
 */
public class Solution {
    /**
     * 执行用时：0 ms, 在所有 Java 提交中击败了100.00%的用户
     * 内存消耗：39 MB, 在所有 Java 提交中击败了25.25%的用户
     * @param board
     * @return
     */
    public int numRookCaptures(char[][] board) {
        int res = 0;
        for (int i = 0; i < board.length; i++) {
            for (int i1 = 0; i1 < board[i].length; i1++) {
                if (board[i][i1] == 'R') {
                    for (int j = i - 1; j >= 0; j--) {
                        if (board[j][i1] == 'p') {
                            res++;
                            break;
                        } else if (board[j][i1] == 'B') {
                            break;
                        }
                    }
                    for (int j = i1 + 1; j < board[i].length; j++) {
                        if (board[i][j] == 'p') {
                            res++;
                            break;
                        } else if (board[i][j] == 'B') {
                            break;
                        }
                    }
                    for (int j = i + 1; j < board[i].length; j++) {
                        if (board[j][i1] == 'p') {
                            res++;
                            break;
                        } else if (board[j][i1] == 'B') {
                            break;
                        }
                    }
                    for (int j = i1 - 1; j >= 0; j--) {
                        if (board[i][j] == 'p') {
                            res++;
                            break;
                        } else if (board[i][j] == 'B') {
                            break;
                        }
                    }
                    return res;
                }
            }
        }
        return 0;
    }
}
